//
//  DynamicTableViewCell.m
//  UI_Dynamic
//
//  Created by dlios on 15-1-16.
//  Copyright (c) 2015年 songCheng. All rights reserved.
//

#import "DynamicTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "SDImageCache.h"
@implementation DynamicTableViewCell

- (void)dealloc
{
    [_dynamicImageView release];
    [_dynamicLabel release];
    [_trackTitleLabel release];
    [_avatarImageView release];
    [_nickname release];
    [_timeline release];
    [super dealloc];
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubview];
    }
    return self;
}

- (void)createSubview
{
    self.dynamicLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 25, 160, 30)];
//    self.dynamicLabel.backgroundColor = [UIColor redColor];
    self.dynamicLabel.numberOfLines = 0;
    self.dynamicLabel.font = [UIFont systemFontOfSize:14];
    [self.contentView addSubview:self.dynamicLabel];
    [_dynamicLabel release];
    
    
    self.trackTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.dynamicLabel.frame.origin.x, self.dynamicLabel.frame.origin.y + 30, self.dynamicLabel.frame.size.width + 50, self.dynamicLabel.frame.size.height)];
    
    self.trackTitleLabel.font = [UIFont systemFontOfSize:14];
    
    self.trackTitleLabel.textColor = [UIColor lightGrayColor];
    
    self.trackTitleLabel.numberOfLines = 0;
//    self.trackTitleLabel.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:self.trackTitleLabel];
    [_trackTitleLabel release];
    
    self.timeline = [[UILabel alloc] initWithFrame:CGRectMake(self.dynamicLabel.frame.origin.x, self.dynamicLabel.frame.origin.y + 50, 160, self.dynamicLabel.frame.size.height)];

    self.timeline.textColor = [UIColor lightGrayColor];
    
    self.timeline.font = [UIFont systemFontOfSize:14];
    
    [self.contentView addSubview:self.timeline];
    [_timeline release];
    
    
    
    
    self.dynamicImageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 15, 100, 120)];
    
   

    self.dynamicImageView.layer.cornerRadius = 20;
    
    self.dynamicImageView.layer.masksToBounds = YES;
    
    [self.contentView addSubview:self.dynamicImageView];
    [_dynamicImageView release];
    
    
    self.audioButton = [[AudioButton alloc] initWithFrame:CGRectMake(15, 40, 80, 80)];
    
    [self.contentView addSubview:self.audioButton];
    
    
    self.avatarImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.trackTitleLabel.frame.origin.x, self.trackTitleLabel.frame.origin.y + 50,20, 20)];

    self.avatarImageView.layer.cornerRadius = 10;
    
    self.avatarImageView.layer.masksToBounds = YES;
    
    [self.contentView addSubview:self.avatarImageView];
    
    [_avatarImageView release];
    
    self.nickname = [[UILabel alloc] initWithFrame:CGRectMake(self.avatarImageView.frame.origin.x + 25 , self.avatarImageView.frame.origin.y, 150, 20)];
    self.nickname.font = [UIFont systemFontOfSize:14];
    
    self.nickname.textColor = [UIColor lightGrayColor];
    
    [self.contentView addSubview:self.nickname];
    [_nickname release];
    
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    self.button.frame = CGRectMake(self.nickname.frame.origin.x + 150, self.nickname.frame.origin.y, 50, 30);

    [self.button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    self.button.layer.cornerRadius = 5;
    
    self.button.layer.masksToBounds = YES;
    
    self.button.layer.borderWidth = 1;
    
    self.button.layer.borderColor = [UIColor redColor].CGColor;

    [self.button setTitle:@"关注" forState:UIControlStateNormal];
    
    [self.contentView addSubview:self.button];
    
}

- (void)setModel:(DynamicModel *)model
{
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }

    self.dynamicLabel.text = model.albumTitle;
    self.trackTitleLabel.text = model.trackTitle;
    self.nickname.text = model.nickname;

  

    
    NSNumber *lastUpdateAt = model.lastUpdateAt;
    
    NSDate *date = [[[NSDate alloc]initWithTimeIntervalSince1970:[lastUpdateAt doubleValue]/1000.0]autorelease]; //把毫秒的时间转化成时间
    
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    
    dateFormatter.dateFormat = @"yyyy-MM-dd";
    
    NSString *date2 = [dateFormatter stringFromDate:date];  //把时间进行格式化
    
    self.timeline.text = [NSString stringWithFormat:@"更新时间:%@",date2];
    
    NSURL *url = [NSURL URLWithString:_model.albumCover];
    
    NSURL *url1 = [NSURL URLWithString:_model.avatar];
    
    [self.avatarImageView sd_setImageWithURL:url1];
    
    [self.dynamicImageView sd_setImageWithURL:url];
    
  
    
}

















- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
